package com.yellowpage.bigdata.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.bigdata.data.NspFatallogNumDayregionSum;
import com.yellowpage.bigdata.service.NspFatallogNumDayregionSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class NspFatallogNumDayregionSumAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	private RegionService regionService;
	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspFatallogNumDayregionSum> nspFatallogNumDayregionSumList = new ArrayList<NspFatallogNumDayregionSum>();
	private List<Map> resultList = new ArrayList<Map>();

	private NspFatallogNumDayregionSum nspFatallogNumDayregionSum;
	private NspFatallogNumDayregionSumService nspFatallogNumDayregionSumService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_logDate;
	private String query_createTime;
	private String query_note;
	private String query_regionCode;
	private String query_numnAllCount;
	private String query_fatallogNumDayregionSumId;
	private String query_numnDayCount;
	private String query_requestType;
	private String query_numAllCount;
	private String query_numDayCount;
	private String query_logDate_end;

	private Map<String, String> channelMap = new HashMap<String, String>();
	private Map<String, String> requestTypeList;
	private Map requestTypeMap = new HashMap();
	private String flag;
	private String channelCode;
	private String regionName;
	private Map<String, String> regionMap;
	public static boolean isType = false;

	
	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}
    public String getRegionSql(){
    	  Map session = ActionContext.getContext().getSession();
    	  User user = (User) session.get("userObject");
    	  String function_code= "OMC_ABILITYCALL_LOG_COUNT_DAY_CHECKCOUNT";
    	  String regionsql= regionService.getRegionclauseByUserFunction(user.getUserId(), function_code, "REGION_CODE");
    	 return regionsql;
    }
	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		//渠道 and请求类型
		channelMap = nspFatallogNumDayregionSumService.getChannelMap();
		String str = "(";
		if(!channelCode.equals("") && query_requestType.equals("")){
			String val[] = channelCode.split(";");
			for(String ss : val){
				for(String key : channelMap.keySet()){
					if(ss.equals(channelMap.get(key))){
						str += "'" + key + "',";
					}
				}
			}
			isType=true;
			str= str.substring(0, str.length() - 1)+")";
			queryMap.put("requestType", str);
		}else if(!query_requestType.equals("")){
				requestTypeList = nspFatallogNumDayregionSumService
						.getrequestTypeMap();
				String[] val = query_requestType.split(";");
				for (String str1 : val) {
					for (String ss : requestTypeList.keySet()) {
						if (requestTypeList.get(ss).equals(str1)) {
							str += "'" + ss + "',";
							break;
						}
					}
				}
				isType=false;
				str= str.substring(0, str.length() - 1)+")";
			queryMap.put("requestType", str);
		}else{
				isType=false;
			queryMap.put("requestType", query_requestType);
		}
		//地区
		if(!regionName.equals("")){
			regionMap = nspFatallogNumDayregionSumService.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			str = "(";
			for(String ss : val){
				for(String key : regionMap.keySet()){
					if(regionMap.get(key).contains(ss)){
						str += "'" + key + "',";
						break;
					}
				}
			}
			str= str.substring(0, str.length() - 1)+")";
			queryMap.put("regionCode", str);
		}else{
			queryMap.put("regionCode", regionName);
		}
		queryMap.put("lodDate", query_logDate);
		queryMap.put("logDate_end", query_logDate_end);
		resultList = nspFatallogNumDayregionSumService
				.queryNspFatallogNumDayregionSumByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String queryChannel(){
		initPageRender();
		if(flag.equals("1")){
			requestTypeMap = nspFatallogNumDayregionSumService.getrequestTypeMap();
		}else if(flag.equals("0")){
			channelMap = nspFatallogNumDayregionSumService.getChannelMap();
		}else{
			regionMap = nspFatallogNumDayregionSumService.getRegionMap(getRegionSql());
		}
		setPageRenderToRequest();
		return flag.equals("1") ? "toRequest":(flag.equals("0") ? "toChannel" : "toRegion");
	}
	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

	public Map<String, String> getChannelMap() {
		return channelMap;
	}

	public void setChannelMap(Map<String, String> channelMap) {
		this.channelMap = channelMap;
	}

	public Map<String, String> getRequestTypeList() {
		return requestTypeList;
	}

	public void setRequestTypeList(Map<String, String> requestTypeList) {
		this.requestTypeList = requestTypeList;
	}

	public Map getRequestTypeMap() {
		return requestTypeMap;
	}

	public void setRequestTypeMap(Map requestTypeMap) {
		this.requestTypeMap = requestTypeMap;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getChannelCode() {
		return channelCode;
	}

	public void setChannelCode(String channelCode) {
		this.channelCode = channelCode;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}

	public static boolean isType() {
		return isType;
	}

	public static void setType(boolean isType) {
		NspFatallogNumDayregionSumAction.isType = isType;
	}

	public Configuration getConfiguration() {
		return configuration;
	}

	public NspFatallogNumDayregionSumService getNspFatallogNumDayregionSumService() {
		return nspFatallogNumDayregionSumService;
	}

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspFatallogNumDayregionSum = nspFatallogNumDayregionSumService
					.getNspFatallogNumDayregionSum(Long.parseLong(selectedId));
		}
	}


	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspFatallogNumDayregionSumService
				.saveNspFatallogNumDayregionSum(nspFatallogNumDayregionSum);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspFatallogNumDayregionSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogNumDayregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogNumDayregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogNumDayregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}

		this.setValidatorResult("/bigdata/NspFatallogNumDayregionSumAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspFatallogNumDayregionSum = nspFatallogNumDayregionSumService
				.getNspFatallogNumDayregionSum(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspFatallogNumDayregionSumService
				.updateNspFatallogNumDayregionSum(nspFatallogNumDayregionSum);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspFatallogNumDayregionSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspFatallogNumDayregionSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		if (nspFatallogNumDayregionSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspFatallogNumDayregionSum.getRequestType().length() == 0) {
			this.addActionError("** RequestType 不能为空 !");
		}
		this.setValidatorResult("/bigdata/NspFatallogNumDayregionSumUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspFatallogNumDayregionSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲�?删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspFatallogNumDayregionSum(
			NspFatallogNumDayregionSum nspFatallogNumDayregionSum) {
		this.nspFatallogNumDayregionSum = nspFatallogNumDayregionSum;
	}

	public NspFatallogNumDayregionSum getNspFatallogNumDayregionSum() {
		return nspFatallogNumDayregionSum;
	}

	public void setNspFatallogNumDayregionSumService(
			NspFatallogNumDayregionSumService nspFatallogNumDayregionSumService) {
		this.nspFatallogNumDayregionSumService = nspFatallogNumDayregionSumService;
	}

	public List<NspFatallogNumDayregionSum> getNspFatallogNumDayregionSumList() {
		return nspFatallogNumDayregionSumList;
	}

	public void setNspFatallogNumDayregionSumList(
			List<NspFatallogNumDayregionSum> nspFatallogNumDayregionSumList) {
		this.nspFatallogNumDayregionSumList = nspFatallogNumDayregionSumList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_logDate() {
		return this.query_logDate;
	}

	public void setQuery_logDate(String logDate) {
		this.query_logDate = logDate;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_numnAllCount() {
		return this.query_numnAllCount;
	}

	public void setQuery_numnAllCount(String numnAllCount) {
		this.query_numnAllCount = numnAllCount;
	}

	public String getQuery_fatallogNumDayregionSumId() {
		return this.query_fatallogNumDayregionSumId;
	}

	public void setQuery_fatallogNumDayregionSumId(
			String fatallogNumDayregionSumId) {
		this.query_fatallogNumDayregionSumId = fatallogNumDayregionSumId;
	}

	public String getQuery_numnDayCount() {
		return this.query_numnDayCount;
	}

	public void setQuery_numnDayCount(String numnDayCount) {
		this.query_numnDayCount = numnDayCount;
	}

	public String getQuery_requestType() {
		return this.query_requestType;
	}

	public void setQuery_requestType(String requestType) {
		this.query_requestType = requestType;
	}

	public String getQuery_numAllCount() {
		return this.query_numAllCount;
	}

	public void setQuery_numAllCount(String numAllCount) {
		this.query_numAllCount = numAllCount;
	}

	public String getQuery_numDayCount() {
		return this.query_numDayCount;
	}

	public void setQuery_numDayCount(String numDayCount) {
		this.query_numDayCount = numDayCount;
	}

}
